package com.itheima.controller;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Repair;
import com.itheima.pojo.Result;
import com.itheima.service.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/repairs")
public class RepairController {
    @Autowired
    private RepairService repairService;

    //                      1.查！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10")   Integer pageSize){
       log.info("分页查询，参数:{},{}",page,pageSize);
       //调用service方法
        PageBean pageBean=repairService.page(page,pageSize);
        return Result.success(pageBean);
    }


    //                    2.删！！！！！！！！！！！！！！！！！！！！！！！！！！！！！
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作");
        repairService.delete(ids);
        return Result.success();
    }

    //                   3.增！！！！！！！！！！！！！！！！！！！！！！！！！！！
    @PostMapping
            public Result save(@RequestBody Repair repair){
                log.info("新增报修记录，repair：{}",repair);
                repairService.save(repair);
                return Result.success();
            }

            @GetMapping("/{id}")
            public Result getById(@PathVariable Integer id){
                log.info("根据ID查询报修记录，id:{}",id);
                Repair repair=repairService.getById(id);
                return Result.success(repair);
    }
    @PutMapping
    public Result update(@RequestBody Repair repair){
        log.info("更新员工信息：{}",repair);
        repairService.update(repair);
        return Result.success();
    }
}
